﻿using FastExecutor.Base.Design;
using FastExecutor.Base.Model;
using FastExecutor.ORG.DB;
using FastExecutor.ORG.Entity;
using FastIOC.Annotation;
using FastORM.Applicaiton;
using FastORM.Util;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace FastExecutor.ORG.Controller
{
    [RouteArea("Frame")]
    [RoutePrefix("Column")]
    public class TableStructController : FastTemplateController<Frame_TableStruct>
    {
        [AutoWired]
        public DB_Table @DB_Table;

        [Route("List")]
        public ActionResult List()
        {
            return View("~/Views/Frame/DMP/Column_List.cshtml");
        }
        [Route("Add")]
        public ActionResult Add()
        {
            return View("~/Views/Frame/DMP/Column_Add.cshtml");
        }
        [Route("Edit")]
        public ActionResult Edit()
        {
            return View("~/Views/Frame/DMP/Column_Edit.cshtml");
        }

        [Route("Script")]
        public ActionResult Script()
        {
            return View("~/Views/Frame/DMP/Column_Script.cshtml");
        }


        [HttpPost, Route("FindList")]
        public override ActionResult FindList()
        {
            JObject Params = GetParams();
            string TableGuid = Convert.ToString(Params["TableGuid"]);
            string FieldName = Convert.ToString(Params["FieldName"]);
            ListWhereText += string.Format(" and IsSys=0");
            if (!string.IsNullOrEmpty(TableGuid))
            {
                ListWhereText += string.Format(" and TableGuid='{0}'", TableGuid);
            }
            if (!string.IsNullOrEmpty(FieldName))
            {
                ListWhereText += string.Format(" and FieldName like '%{0}%'", FieldName);
            }
            return base.FindList();
        }

        [HttpPost, Route("FindDetail")]
        public override ActionResult FindDetail()
        {
            return base.FindDetail();
        }

        [HttpPost, Route("OperateAdd")]
        public override ActionResult OperateAdd(Frame_TableStruct Entity)
        {
            ReturnValue result = new ReturnValue();
            DBRow row = new DBRow("Frame_TableStruct", "SQLFieldName", Entity.SQLFieldName);
            if (row.IsExist())
            {
                result.Add("code", -100);
                result.Add("msg", "已存在相同名称的列！");
                return result;
            }
            else
            {
                return base.OperateAdd(Entity);
            }

        }

        [HttpPost, Route("OperateUpdate")]
        public override ActionResult OperateUpdate()
        {
            return base.OperateUpdate();
        }

        [HttpPost, Route("OperateBatchDelete")]
        public override ActionResult OperateBatchDelete()
        {
            return base.OperateBatchDelete();
        }


        [HttpPost, Route("CreatColumnAddScript")]
        public ActionResult CreatColumnAddScript()
        {
            ReturnValue result = new ReturnValue();
            JObject Params = GetParams();
            string TableGuid = Convert.ToString(Params["TableGuid"]);
            Frame_Table Table = @DB_Table.FindOne(TableGuid);
            string[] KeyGuidList = Convert.ToString(Params["KeyGuidList"]).Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            string ScriptStr = string.Empty;
            PropertyInfo[] Props = MapperBuilder.GetInstance().GetMap(typeof(Frame_TableStruct)).MapPropertyList;
            foreach (string KeyGuid in KeyGuidList)
            {

                Frame_TableStruct Entity = DB_Fast.FindOne(KeyGuid);
                string DDLSQL = string.Empty;
                switch (Entity.FieldType)
                {
                    case "nvarchar":
                        DDLSQL = string.Format("{0}({1})", Entity.FieldType, Entity.FieldLength);
                        break;
                    case "numeric":
                        DDLSQL = string.Format("{0}(10,{1})", Entity.FieldType, Entity.FieldLength);
                        break;
                    case "blob":
                    case "longtext":
                    case "int":
                    case "datetime":
                        DDLSQL = string.Format("{0}", Entity.FieldType);
                        break;
                }
                string FinalDDLSQL = string.Format("ALTER TABLE {0} ADD {1} {2}",
                        Table.SQLTableName, Entity.SQLFieldName, DDLSQL);

                string DBSQL = string.Format("INSERT INTO {0} ", "Frame_TableStruct");
                string ColumnsStr = " (";
                string ValueStr = "(";
                foreach (PropertyInfo p in Props)
                {
                    ColumnsStr += p.Name + ",";
                    ValueStr += "'" + Convert.ToString(MapperBuilder.GetInstance().GetMap(typeof(Frame_TableStruct)).EmitDic[p.Name].EmitGetter(Entity)) + "',";
                }
                ColumnsStr = ColumnsStr.Trim(',') + ") ";
                ValueStr = ValueStr.Trim(',') + ") ";
                ScriptStr += FinalDDLSQL + ";" + Environment.NewLine;
                ScriptStr += DBSQL + ColumnsStr + "values" + ValueStr + ";" + Environment.NewLine;
            }
            result.Add("Script", ScriptStr);
            return result;
        }
    }
}
